package initialize

import (
	"fmt"
	_ "github.com/mbobakov/grpc-consul-resolver"
	"google.golang.org/grpc"
	"log"
	"order_bff/global"
	"order_bff/goods_proto"
	"order_bff/inventory_proto"
	"order_bff/order_proto"
)

func InitSrvConnect() {
	//订单srv
	OrderConn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", global.ServerConfig.ConsulConfig.ConsulHost, global.ServerConfig.ConsulConfig.ConsulPort, global.ServerConfig.ConsulConfig.OrderSrvName),
		grpc.WithInsecure(),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		log.Fatal(err)
	}
	global.OrderSrvConnect = order_proto.NewOrderClient(OrderConn)
	//商品srv
	GoodsConn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", global.ServerConfig.ConsulConfig.ConsulHost, global.ServerConfig.ConsulConfig.ConsulPort, global.ServerConfig.ConsulConfig.GoodsSrvName),
		grpc.WithInsecure(),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		log.Fatal(err)
	}
	global.GoodsSrvConnect = goods_proto.NewGoodsClient(GoodsConn)
	//库存srv
	InventoryConn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s", global.ServerConfig.ConsulConfig.ConsulHost, global.ServerConfig.ConsulConfig.ConsulPort, global.ServerConfig.ConsulConfig.InventorySrvName),
		grpc.WithInsecure(),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		log.Fatal(err)
	}
	global.InventorySrvConnect = inventory_proto.NewInventoryClient(InventoryConn)
}
